using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Users {
    class UserContactRequestBC : IUserContactRequest {

        #region IUserContactRequest Members

        public bool SendViewContacInfoRequest(UserContactRequestEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (UserDA.GetByUserKey(item.RequestSenderUserKey, connectionName, null, null) == null
                    ||
                    UserDA.GetByUserKey(item.RequestReceiverUserKey, connectionName, null, null) == null
                    ||
                    UserContactRequestDA.GetByRequestSenderUserKeyRequestReceiverUserKey(item.RequestSenderUserKey, item.RequestReceiverUserKey, ConfigInfo.ConnectionName, null, null) != null
                    ) {
                    return false;
                }

                //Normalize data
                if (item.DateCreated == DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert to DB
                result = UserContactRequestDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.SendViewContacInfoRequest.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool SendViewContacInfoRequest(string requestSenderUserKey, string requestReceiverUserKey) {
            return SendViewContacInfoRequest(new UserContactRequestEnt() { RequestSenderUserKey = requestSenderUserKey, RequestReceiverUserKey = requestReceiverUserKey, DateCreated = DateTime.Now });
        }

        public bool AllowRequest(UserContactedEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                if (item.DateCreated == DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert to DB
                result = UserContactedDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Delete the request
                if (result) {
                    result = UserContactRequestDA.Delete(item.AllowedUserKey, item.UserKey, connectionName, dbCon, dbTrans) > 0;
                }

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserContactRequestBC.AllowRequest({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool AllowRequest(string userKey, string allowedUserKey) {
            return AllowRequest(new UserContactedEnt() { UserKey = userKey, AllowedUserKey = allowedUserKey, DateCreated = DateTime.Now });
        }

        public bool DenyRequest(UserContactRequestEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Delete the request
                result = UserContactRequestDA.Delete(item.RequestSenderUserKey, item.RequestReceiverUserKey, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserContactRequestBC.DenyRequest({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool DenyRequest(string requestSenderUserKey, string requestReceiverUserKey) {
            return DenyRequest(new UserContactRequestEnt() { RequestSenderUserKey = requestSenderUserKey, RequestReceiverUserKey = requestReceiverUserKey });
        }

        public bool RemoveUserFromAllowedList(UserContactedEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Delete the allowed user from list
                result = UserContactedDA.Delete(item.UserKey, item.AllowedUserKey, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserContactRequestBC.RemoveUserFromAllowedList({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool RemoveUserFromAllowedList(string userKey, string removedUserKey) {
            return RemoveUserFromAllowedList(new UserContactedEnt() { UserKey = userKey, AllowedUserKey = removedUserKey });
        }

        public bool CheckRequestSended(string requestSenderUserKey, string requestReceiverUserKey) {
            try {
                if (UserContactRequestDA.GetByRequestSenderUserKeyRequestReceiverUserKey(requestSenderUserKey, requestReceiverUserKey, ConfigInfo.ConnectionName, null, null) == null) {
                    return false;
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserContactRequestBC.CheckRequestSended({0}, {1}): \n" + ex.ToString(),
                                   requestSenderUserKey, requestReceiverUserKey));
            }
            return true;
        }

        public bool CheckUserHaveRightViewContactInfo(string requestSenderUserKey, string requestReceiverUserKey) {
            try {
                if (UserContactedDA.GetByUserKeyAlllowedUserKey(requestReceiverUserKey, requestSenderUserKey, ConfigInfo.ConnectionName, null, null) == null) {
                    return false;
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserContactRequestBC.CheckUserHaveRightViewContactInfo({0}, {1}): \n" + ex.ToString(),
                                   requestSenderUserKey, requestReceiverUserKey));
            }
            return true;
        }

        public List<UserContactRequestEnt> GetRequestingListPaging(string requestReceiverUserKey, int page, int recordPerPage, out int totalRecord) {
            List<UserContactRequestEnt> result = new List<UserContactRequestEnt>();
            totalRecord = 0;
            try {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    totalRecord = UserContactRequestDA.CountByRequestReceiverUserKey(requestReceiverUserKey, connectionName, null, null);
                    if (totalRecord == 0) {
                        return result;
                    }

                    int totalPage = (int)(totalRecord / recordPerPage);
                    if (totalRecord % recordPerPage != 0) {
                        totalPage++;
                    }
                    if (page > totalPage) {
                        return result;
                    }

                    long startRow = (page - 1) * recordPerPage + 1;
                    long endRow = startRow + recordPerPage - 1;

                    if (endRow > totalRecord) {
                        endRow = totalRecord;
                    }

                    result = UserContactRequestDA.GetByRequestReceiverUserKeyWithPaging(requestReceiverUserKey, startRow, endRow, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserGeneralProfileBC.GetRequestingListPaging({0}, {1}, {2}): \n" + ex.ToString(),
                                                requestReceiverUserKey,
                                                page,
                                                recordPerPage));
                totalRecord = 0;
            }
            return result;
        }

        public List<UserContactedEnt> GetAllowedListPaging(string userKey, int page, int recordPerPage, out int totalRecord) {
            List<UserContactedEnt> result = new List<UserContactedEnt>();
            totalRecord = 0;
            try {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    totalRecord = UserContactedDA.CountByUserKey(userKey, connectionName, null, null);
                    if (totalRecord == 0) {
                        return result;
                    }

                    int totalPage = (int)(totalRecord / recordPerPage);
                    if (totalRecord % recordPerPage != 0) {
                        totalPage++;
                    }
                    if (page > totalPage) {
                        return result;
                    }

                    long startRow = (page - 1) * recordPerPage + 1;
                    long endRow = startRow + recordPerPage - 1;

                    if (endRow > totalRecord) {
                        endRow = totalRecord;
                    }

                    result = UserContactedDA.GetByUserKeyWithPaging(userKey, startRow, endRow, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserGeneralProfileBC.GetRequestingListPaging({0}, {1}, {2}): \n" + ex.ToString(),
                                                userKey,
                                                page,
                                                recordPerPage));
                totalRecord = 0;
            }
            return result;
        }

        #endregion
    }
}